<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/6
 * Time: 17:09
 */

namespace Portal\Controller;

use Common\Controller\AdminbaseController;

class ProjectsController extends AdminbaseController
{
    function _initialize() {
        parent::_initialize();
    }
    public function index(){
        $result=M('projects')
            ->alias("a")
            ->field('a.*,b.name')
            ->join("__PROJECTS_CAT__ b ON a.c_id = b.id")
            ->select();
        $this->assign('result',$result);
        $this->display();
    }
    public function add(){
        $re=M('projects_cat')->select();
        $this->assign('result',$re);
        $this->display();
    }
    public function add_post(){
        $data['img'] = sp_asset_relative_url(I('post.img'));
        $data['title'] = I('post.title');
        $data['c_id'] = I('post.c_id');
        $data['text'] = htmlspecialchars_decode(I('post.text'));
        $data['createtime'] = I('post.createtime');
        !empty($data['img'])?:$this->error('请上传图片');
        if(M('Projects')->add($data)){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }
    public function edit(){
        $id=I('get.id');
        $result=M('projects')
            ->alias("a")
            ->field('a.*,b.name')
            ->join("__PROJECTS_CAT__ b ON a.c_id = b.id")
            ->where("a.id=$id")
            ->find();
        $this->assign('result',$result);
        $cat=M('projects_cat')->select();
        $this->assign('cat',$cat);
        $this->display();
    }
    public function edit_post(){
        $id=I('post.id');
        $data['img'] = sp_asset_relative_url(I('post.img'));
        $data['title'] = I('post.title');
        $data['c_id'] = I('post.c_id');
        $data['text'] = htmlspecialchars_decode(I('post.text'));
        $data['createtime'] = I('post.createtime');
        if(M('projects')->where("id = $id")->save($data)){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
    public function delete(){
        $id=I('get.id');
        if(M('projects')->where("id = $id")->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    public function cat_index(){
        $re=M('projects_cat')->select();
        $this->assign('result',$re);
        $this->display();
    }
    public function cat_add(){
        $this->display();
    }
    public function cat_add_post(){
        $data['name']=I('post.name');
        if(M('projects_cat')->add($data)){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
        $this->display();
    }
    public function cat_edit(){
        $id=I('get.id');
        $re=M('projects_cat')->where("id = $id")->find();
        $this->assign('result',$re);
        $this->display();
    }
    public function cat_edit_post(){
        $id=I('post.id');
        $data['name']=I('post.name');
        if(M('projects_cat')->where("id=$id")->save($data)){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
    public function cat_delete(){
        $id=I('get.id');
        if(M('projects_cat')->where("id = $id")->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}